<?php

namespace backend\models\config;

use common\models\config\AdminMenuModel;

/**
 * Desc 菜单
 */
class AdminMenu extends AdminMenuModel
{

    /**
     * 获取单条数据
     * @param array $condition 搜索条件 如：$condition = ['and'];$condition[] = ['=','id',1];$condition[] = ['LIKE','name','dkh'];
     * @param string $order_by 排序 
     * @param array $select  排获取字段
     * @return array
     * * */
    public static function getDataOne($condition = [], $order_by = 'id DESC', $select = [])
    {
        $query = self::find()->select($select);
        $query->orderBy($order_by);
        if ($condition) {
            $query->where($condition);
        }
        return $query->asArray()->one();
    }

    /**
     * 获取多条数据
     * @param array $condition [AND,['=','id',1],['','',''],...] 搜索条件
     * @param int $page 页数 
     * @param int $page_size 每页总数 
     * @param string $order_by 排序 
     * @param array $select  排获取字段
     * @return array
     * * */
    public static function getDataAll($condition = [], $page = 1, $page_size = 20, $order_by = 'id DESC', $select = [])
    {
        $query = self::find()->select($select);
        list($offset, $limit) = \common\tools\Util::getLimit($page, $page_size);
        if ($condition) {
            $query->where($condition);
        }
        $data['total'] = intval($query->count());
        $query->offset($offset)->limit($limit)->orderBy($order_by);
        $data['list'] = $query->asArray()->all();
        return $data;
    }

    /**
     * 新增/编辑
     * @param array $params
     * * */
    public static function create($params)
    {
        if (!is_array($params)) {
            return false;
        }
        // $params['create_time'] = date('Y-m-d H:i:s');
        // $params['update_time'] = date('Y-m-d H:i:s');
        $model = new self();
        $model->setAttributes($params, false);
        return $model->save();
    }

    /**
     * 根据条件更新对应的字段
     * @param array $params
     * @param array $condition
     * * */
    public static function updateData($params, $condition)
    {
        if (empty($condition) || empty($params)) {
            return false;
        }
        if (!is_array($condition) || !is_array($params)) {
            return false;
        }
        if (empty(self::find()->where($condition)->all())) {
            return false;
        }
        // $params['update_time'] = date('Y-m-d H:i:s');
        return self::updateAll($params, $condition);
    }

    /**
     * 根据条件删除
     * @param array $condition
     * * */
    public static function del($condition)
    {
        if (empty($condition)) {
            return false;
        }
        if (empty(self::find()->where($condition)->all())) {
            return false;
        }
        return self::deleteAll($condition);
    }

    /**
     * 获取用户角色
     * @param int $menu_id ['1,2,3,4','3,5,6'] 菜单ID
     * **/
    public static function getMenuByRole($menu_id)
    {
        if (empty($menu_id)) {
            return [];
        }
        $id_arr = [];
        foreach ($menu_id as $value) {
            if ($value) {
                $id_arr = array_merge($id_arr, explode(',', $value));
            }
        }
        $condition = ['AND'];
        $condition[] = ['IN', 'id', $id_arr];
        $data = self::getDataAll($condition, 1, 200);
        return $data['list'];
    }

    /**
     * @desc 递归获取树状菜单
     * @return string
     * */
    public static function arrayTree($ary = [], $route_parent_id = 0, $level = 10, $i = 1)
    {
        $arr = [];
        foreach ($ary as &$rs) {
            if ($rs['parent'] != $route_parent_id) {
                continue;
            }
            if ($i > $level) {
                break;
            }

            $arr[$rs['id']] = $rs;
            $n = $i;
            $n++;
            $children = self::arrayTree($ary, $rs['id'], $level, $n);
            if ($children) {
                $arr[$rs['id']]['children'] = $children;
            } else {
                $arr[$rs['id']]['children'] = [];
            }
        }
        if ($arr) {
            $arr = array_values($arr);
        }
        return $arr;
    }
}
